package xyz.xtt.pay.mq;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.asset.api.enums.OwnerTypeEnum;
import xyz.xtt.asset.client.response.AssetInfoResponse;
import xyz.xtt.pay.api.dto.PayAttach;
import xyz.xtt.pay.api.enums.PayOrderBusinessTypeEnum;
import xyz.xtt.pay.client.response.TblPayOrderDto;
import xyz.xtt.pay.service.IInnerService;
import xyz.xtt.pay.service.ITblPayLimitService;

/**
 * 支付单取消后消息消费
 *
 * @author dale
 * @since 2025/2/18
 **/
@RefreshScope
@Slf4j
@Component
@RocketMQMessageListener(topic = "${rocketmq.pay.order.status.change.topic:nft_pay_order_status_change}",
		selectorExpression = "BUY_ASSET_1155:PAY_CANCEL||C_TO_C:PAY_CANCEL",
		consumerGroup = "${rocketmq.consumer.group.afterPayCancel}")
public class AfterPayCancelConsumer implements RocketMQListener<MessageExt> {
	@Autowired
	private ITblPayLimitService iTblPayLimitService;
	@Autowired
	private IInnerService iInnerService;

	@Override
	public void onMessage(MessageExt msg) {
		String msgId = msg.getMsgId();
		byte[] body = msg.getBody();
		String message = new String(body, StandardCharsets.UTF_8);
		log.info("AfterPayCancelConsumer nft_pay_order_status_change msgId:{} message: {}", msgId, message);
		if (StringUtils.isEmpty(message)) {
			return;
		}
		if (!JSONUtil.isTypeJSON(message)) {
			log.warn("AfterPayCancelConsumer nft_pay_order_status_change Received message: {} is not json", message);
			return;
		}
		try {
			TblPayOrderDto payOrderDto = JSONUtil.toBean(message, TblPayOrderDto.class);
			if (Objects.isNull(payOrderDto)) {
				log.warn("AfterPayCancelConsumer nft_pay_order_status_change JSONUtil.toBean is null. message:{}", message);
				return;
			}else if (StringUtils.isBlank(payOrderDto.getBusinessAttach())){
				log.warn("AfterPayCancelConsumer nft_pay_order_status_change businessAttach is null. message:{}", message);
				return;
			}
			PayAttach payAttach = JSONUtil.toBean(payOrderDto.getBusinessAttach(), PayAttach.class);
			Long assetId = payAttach.getAssetId();
			if (assetId == null){
				log.warn("AfterPayCancelConsumer nft_pay_order_status_change assetId is null. message:{}",message);
				return;
			}
			AssetInfoResponse tblAsset = iInnerService.infoWithAssetId(payAttach.getAssetId());
			if ((Objects.equals(payOrderDto.getBusinessType(), PayOrderBusinessTypeEnum.BUY_ASSET_1155.getCode())
					&& Objects.equals(tblAsset.getOwnerType(), OwnerTypeEnum.USER.code()))
					|| Objects.equals(payOrderDto.getBusinessType(), PayOrderBusinessTypeEnum.C_TO_C.getCode())){
					iTblPayLimitService.cancelUserPayLimit(payOrderDto.getAppUserId(),
							Long.valueOf(tblAsset.getOwnerId()),
							payOrderDto.getPayMoney(),payOrderDto.getCreateTime());
			}

		} catch (Exception e) {
			log.info("PayAfterConsumer consume failed. message:{} ex:{}", message, e.getMessage(), e);
			throw new RuntimeException("PayAfterConsumer Consumer Message exception", e);
		}
	}
}
